from django.db import connection

from commonTool.logs.logs_wrapper import LogUtil

logs = LogUtil()


class SQLPrintingMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    @logs
    def process_response(self, request, response):
        print("进入执行打印sql过滤器")
        # 打印运行的SQL语句
        for query in connection.queries:
            logs.warning(query['sql'])
            print(query['sql'])

        return response
